<?php
namespace app\admin\service;


use app\common\model\Applet;
use nxcsoft\wechat\Driver;
use think\facade\Db;

/**
 * 小程序管理-服务类
 * @author 小码哥
 * @since: 2023/11/28
 * Class AppletService
 * @package app\admin\service
 */
class AppletService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new Applet();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2023/11/28
     * @author 小码哥
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];


        // 测试名称
        $name = isset($param['name']) ? trim($param['name']) : '';
        if ($name) {
            $map[] = ['name', 'like', "%{$name}%"];
        }

        // 性别
        $gender = isset($param['gender']) ? (int)$param['gender'] : 0;
        if ($gender) {
            $map[] = ['gender', '=', $gender];
        }

        // 类型
        $type = isset($param['type']) ? (int)$param['type'] : 0;
        if ($type) {
            $map[] = ['type', '=', $type];
        }

        // 状态
        $status = isset($param['status']) ? (int)$param['status'] : 0;
        if ($status) {
            $map[] = ['status', '=', $status];
        }

        // 是否VIP
        $is_vip = isset($param['is_vip']) ? (int)$param['is_vip'] : 0;
        if ($is_vip) {
            $map[] = ['is_vip', '=', $is_vip];
        }

        return parent::getList($map); // TODO: Change the autogenerated stub
    }

    /**
     * @return array
     * 生成小程序授权链接
     */
    public function authMiniUrl()
    {
        $param = request()->param();
        $applet_id = isset($param['applet_id']) ? (int)$param['applet_id'] : 0;
        if($applet_id==0){
            $applet_id = $this->createAppletId();
        }
        $wx = new Driver;
//        $type，1=授权公众号，2=授权小程序，3=两者都有
        $url = $wx->authUrl($applet_id, 2);//获取授权页面地址
        return message("操作成功", true, ['auth_url' => $url]);
    }

    /**
     * @return int|string
     * 生成小程序id
     */
    public function createAppletId()
    {
        $data = [
            'source' => 1,
            'status' => 0,
            'create_time' => time(),
            'update_time' => time(),
            'mark' =>1,
        ];
        $applet_id = Db::name('applet')->insertGetId($data);
        return $applet_id;
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2023/11/28
     * @author 小码哥
     */
    public function edit()
    {
        // 参数
        $data = request()->param();

        // 头像处理
        $avatar = getter($data, 'avatar');
        if (!empty($avatar)) {
            if (strpos($avatar, "temp") !== false) {
                $data['avatar'] = save_image($avatar, 'example');
            } else {
                $data['avatar'] = str_replace(IMG_URL, "", $avatar);
            }
        }

        return parent::edit($data); // TODO: Change the autogenerated stub
    }


    /**
     * 设置是否VIP
     * @return array
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     * @since 2023/11/28
     * @author 小码哥
     */
    public function setIsVip()
    {
        // 参数
        $data = request()->param();
        $id = getter($data, "id", 0);
        if (!$id) {
            return message('记录ID不能为空', false);
        }
        $is_vip = getter($data, "is_vip", 0);
        if (!$is_vip) {
            return message('记录是否VIP不能为空', false);
        }
        $error = '';
        $item = [
            'id' => $id,
            'is_vip' => $is_vip];
        $rowId = $this->model->edit($item, $error);
        if (!$rowId) {
            return message($error, false);
        }
        return message();
    }

}